package com.ykg.service.impl;

import com.ykg.dto.ListDTO;
import com.ykg.mapper.ListMapper;
import com.ykg.mapper.OwnerMapper;
import com.ykg.mapper.ShipMapper;
import com.ykg.pojo.ListCharge;
import com.ykg.pojo.Owner;
import com.ykg.pojo.Ship;
import com.ykg.request.AddListRequest;
import com.ykg.service.IDTOService;
import com.ykg.service.IListService;
import com.ykg.util.CreateIdUtil;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * TODO
 * <p>
 * Created on 10:36 2021/6/22.
 *
 * @Author: laobian
 * @Description:
 */
@Service
public class ListServiceImpl implements IListService {

    @Autowired
    private ListMapper listMapper;

    @Autowired
    private IDTOService dtoService;

    @Autowired
    private ShipMapper shipMapper;

    @Override
    /**
    * TODO
    *  获取某个货主的所有卸货清单
    * @params：
        @param ownerId
    * @return {@link List< ListCharge>}
    * @Author: laobian
    * @Date: 21:35 2021/6/23
    */
    public List<ListCharge> findListByOwnerId(Long ownerId) {
        List<ListCharge> listCharges = listMapper.findListByOwnerId(ownerId);
        if(Objects.isNull(listCharges)){
            return null;
        }
        return listCharges;
    }

    @Override
    /**
    * TODO
    *  货主增加一个卸货清单
    * @params：
        @param listCharge
    * @return {@link int}
    * @Author: laobian
    * @Date: 21:38 2021/6/23
    */
    public int addList(AddListRequest addListRequest) {
        CreateIdUtil createIdUtil= new CreateIdUtil();
        Long shipId = shipMapper.findShipByName(addListRequest.getShipName());
        String ListId = createIdUtil.creatListChargeId();
        ListCharge listCharge = new ListCharge(ListId,addListRequest.getOutNum()
        ,shipId,addListRequest.getOwnerId());
        int status = listMapper.addList(listCharge);
        return status;
    }

    @Override
    /**
    * TODO
    *  获取所有的卸货清单
    * @params：
        @param
    * @return {@link List< ListCharge>}
    * @Author: laobian
    * @Date: 21:40 2021/6/23
    */
    public List<ListCharge> findAllList() {
        List<ListCharge> listCharges =listMapper.findAll();
        return listCharges;
    }

    @Override
    /**
    * TODO
    *  获取某个卸货清单详情
    * @params：
      @param listId
    * @return {@link ListDTO}
    * @Author: laobian
    * @Date: 21:40 2021/6/23
    */
    public ListDTO findListByListId(String listId) {
        ListCharge listCharge = listMapper.findListById(listId);
        ListDTO listDTO = dtoService.createListDto(listCharge);
        return listDTO;
    }

    @Override
    /**
    * TODO
    *  更新某个卸货清单的状态
    * @params：
         @param state
    * @return {@link int}
    * @Author: laobian
    * @Date: 21:43 2021/6/23
    */
    public int updateList(String listId,int state) {
        int status = listMapper.updateListState(listId,state);
        return status;
    }
}
